package com.xjt.query.action;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;
import com.xjt.query.dao.pojo.QueryUser;
import com.xjt.query.service.interf.LoginService;

public class LoginAction extends ActionSupport {

    private static final long   serialVersionUID = -2690095510421003286L;

    private static final Logger log              = Logger.getLogger(LoginAction.class);

    private QueryUser           userInfo;

    private String              preUrl;

    private String              savePwd;

    private LoginService        loginService;

    private Map<String, String> errorMap         = new LinkedHashMap<String, String>();

    public String showLogin() {
        return SUCCESS;
    }

    public String doLogin() {
        if (null == userInfo) {
            return LOGIN;
        }
        if (StringUtils.isBlank(userInfo.getUserId())) {
            log.error("login failed! user id can not be empty! ");
            errorMap.put("userId", "用户名不能为空！");
            return INPUT;
        }
        if (StringUtils.isBlank(userInfo.getUserPwd())) {
            log.error("login failed! password can not be empty! userId=" + userInfo.getUserId());
            errorMap.put("userPwd", "密码不能为空！");
            return INPUT;
        }
        QueryUser dbUserInfo = loginService.doLogin(userInfo.getUserId(), userInfo.getUserPwd(), savePwd, errorMap);
        if (null == dbUserInfo) {
            log.error("login failed! user id or password error! userId=" + userInfo.getUserId() + ", password="
                      + userInfo.getUserPwd());
            return INPUT;
        }
        log.error("login success! userId=" + userInfo.getUserId());
        return SUCCESS;
    }

    /**
     * @param loginService the loginService to set
     */
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    public String doLogout() {
        loginService.doLogout();
        return SUCCESS;
    }

    /**
     * @return the userInfo
     */
    public QueryUser getUserInfo() {
        return userInfo;
    }

    /**
     * @param userInfo the userInfo to set
     */
    public void setUserInfo(QueryUser userInfo) {
        this.userInfo = userInfo;
    }

    /**
     * @return the preUrl
     */
    public String getPreUrl() {
        return preUrl;
    }

    /**
     * @param preUrl the preUrl to set
     */
    public void setPreUrl(String preUrl) {
        this.preUrl = preUrl;
    }

    /**
     * @return the savePwd
     */
    public String getSavePwd() {
        return savePwd;
    }

    /**
     * @param savePwd the savePwd to set
     */
    public void setSavePwd(String savePwd) {
        this.savePwd = savePwd;
    }

    /**
     * @return the errorMap
     */
    public Map<String, String> getErrorMap() {
        return errorMap;
    }

    /**
     * @param errorMap the errorMap to set
     */
    public void setErrorMap(Map<String, String> errorMap) {
        this.errorMap = errorMap;
    }

}
